<?php 


/*******************************************************************************
 * 
 * units.lib.php
 * 
 *******************************************************************************
 * 
 * @author Stanislas PERRIN <stanislas.perrin@gmail.com>
 * @licence LGPL v. 3.0
 * 
 *******************************************************************************
 * 
 * Purpose
 * -------
 * Units conversion library.
 * 
 * Note(s)
 * -------
 * => php version 5.2 min. -- 5.3 recommended
 * => Errors are thrown via the php exception system : do not forget 
 * to try and catch while using this lib !
 * 
 * Some functions principles :
 * 
 * => type conservation : if a string is given, a string is returned (but 
 *    computations are made with a float cast)
 *                      : if an array is given, an array is returned by
 *    recursively call the function for each array values.
 *    This recursive call is supposed to work with arrays of arrays of arrays...
 *
 * => ... but objects are rejected !
 * 
 * => about function names : originUnit_to_destinationUnit
 * 
 * Dependancies
 * ------------
 * -> debug.class.php
 * 
 ******************************************************************************/

# ------------------------------------------------------------------------------
# namespace
# ------------------------------------------------------------------------------
# namespace phutile\libs\units; // uncomment only if php version >= 5.3

# ------------------------------------------------------------------------------
# Dependencies inclusions
# ------------------------------------------------------------------------------
// debug.class.php
if (!@include_once('./debug.class.php')) die("impossible to load mandatory file debug.class.php");


# ------------------------------------------------------------------------------
# Locale configuration
# ------------------------------------------------------------------------------
$locale = 'en_EN';

# ------------------------------------------------------------------------------
# Locale file inclusion
# ------------------------------------------------------------------------------
$localeFile = "./locales/$locale.lang.php";
if (!@include_once($localeFile)) throw new debug("impossible to load/include [$localFile]",11);


/**
 * 
 * unitConverter()
 * 
 * @param mixed $firstUnit first unit value to convert
 * @param float $rate conversion rate of the second unit expressed in the first unit
 * @return mixed $firstUnit converted in another unit in its original type
 * 
 */


################################################################################
#
# GENERIC
#
################################################################################

function unitConverter($originUnit,$rate)
{
	// Argument(s) testing
	if (!$originUnit || is_object($originUnit)) throw new debug($_ERROR_CODES['units'][1]);
	
	if (is_array($originUnit))
	{
		foreach ($originUnit as &$value)
		{
			$value = unitConverter($value,$rate);
		}
		
		return $originUnit;
	}
	
	// Conversion
	$destinationUnit = floatval($originUnit) / floatval($rate);
	
	// « Smart Return » depending on argument type
	return (is_float($originUnit)) ? $destinationUnit : strval($destinationUnit);
	
}


################################################################################
#
# LENGTHS / DISTANCES
#
################################################################################

$toMeter = array(
		"meter" => 1.0,
		"inch" => 0.0254,
		"link" => 0.201168,
		"foot" => 0,3048,
		"yard" => 0.9144,
		"rod" => 5.0292,
		"pole" => 5.0292,
		"perch" => 5.0292,
		"chain" => 20,1168,
		"furlong" => 201,168,
		"mile" => 1609,344,
		"league" => 4828,032
		);

/**
 * 
 * convertToMeter()
 * 
 */

/**
 * 
 * meter_to_inch()
 * 
 */
function meter_to_inch($meters=false)
{
	return unitConverter($meters,0.0254);
}

/**
 * 
 * meter_to_foot()
 * 
 * @param $meters mixed meters to convert in foot/feet
 * @return mixed meters converted to foot/feet
 * 
 */
function meter_to_foot($meters=false)
{
	return unitConverter($meters,0.3048);
}

/**
 * 
 * meter_to_yard()
 * 
 */
function meter_to_yard($meters=false)
{
	return unitConverter($meters,0.9144);
}


################################################################################
#
# VOLUMES
#
################################################################################

################################################################################
#
# WEIGHT/MASSES
#
################################################################################

################################################################################
#
# TEMPERATURES
#
################################################################################

################################################################################
#
# ENERGIES
#
################################################################################



?>
